﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace BasicComponents.tree
{
   public class TreeModel : INotifyPropertyChanged,ICloneable
    {
        private Brush foreground = Brushes.Black;
        private object operateObj;
        private String id;
        private String header;
        private String iconPath = null;
        private Boolean isChecked = false;
        private Boolean isExpanded = false;
        private TreeModel parent = null;
        private TreeModel selfCopy = null;
        private ObservableCollection<TreeModel> children = new ObservableCollection<TreeModel>();
        private String toolTip = "";
        private Visibility visibilityCheckBox = Visibility.Visible;
        private Visibility visibilityId = Visibility.Visible;
        private Visibility visibilitySpliter = Visibility.Visible;
        private String spliter = "-"; 
        public string Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Id"));
            }
        }

        public string Header
        {
            get
            {
                return header;
            }

            set
            {
                header = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Header"));
            }
        }
 public object OperateObj
        {
            get
            {
                return operateObj;
            }

            set
            {
                operateObj = value;
                OnPropertyChanged(new PropertyChangedEventArgs("OperateObj"));
            }
        }
        public string IconPath
        {
            get
            {
                return iconPath;
            }

            set
            {
                iconPath = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IconPath"));
            }
        }

        public bool IsChecked
        {
            get
            {
                return isChecked;
            }

            set
            {
                isChecked = value;  
                foreach (TreeModel child in Children) { 
                   // child.isChecked = value;
                    SetChildrenChecked(child,value);
                } 
                OnPropertyChanged(new PropertyChangedEventArgs("IsChecked"));
            }
        }

        public bool IsExpanded
        {
            get
            {
                return isExpanded;
            }

            set
            {
                isExpanded = value; 
                OnPropertyChanged(new PropertyChangedEventArgs("IsExpanded"));
            }
        }

        public TreeModel Parent
        {
            get
            {
                return parent;
            }

            set
            {
                parent = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Parent"));
            }
        }

        public ObservableCollection<TreeModel> Children
        {
            get
            {
                return children;
            }

            set
            {
                children = value;
                foreach (TreeModel child in value) {
                    child.parent = this;
                }
               OnPropertyChanged(new PropertyChangedEventArgs("Children"));
            }
        }

        public string ToolTip
        {
            get
            {
                return toolTip;
            }

            set
            {
                toolTip = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ToolTip"));
            }
        }

        public Visibility VisibilityCheckBox
        {
            get
            {
                return visibilityCheckBox;
            }

            set
            {
                visibilityCheckBox = value;
                OnPropertyChanged(new PropertyChangedEventArgs("VisibilityCheckBox"));
            }
        }

        public string Spliter
        {
            get
            {
                return spliter;
            }

            set
            {
                spliter = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Spliter"));
            }
        }

        public Visibility VisibilityId
        {
            get
            {
                return visibilityId;
            }

            set
            {
                visibilityId = value;
                OnPropertyChanged(new PropertyChangedEventArgs("VisibilityId"));
            }
        }

        public Visibility VisibilitySpliter
        {
            get
            {
                return visibilitySpliter;
            }

            set
            {
                visibilitySpliter = value;
                OnPropertyChanged(new PropertyChangedEventArgs("VisibilitySpliter"));
            }
        }

        public TreeModel SelfCopy
        {
            get
            {
                return this.MemberwiseClone() as TreeModel;
            } 
            set
            {
                selfCopy = value;
            }
        }

        public Brush Foreground
        {
            get {
                return foreground;
            }
            set {
                foreground = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Foreground"));
            }
        }

        public TreeModel() {
            Children = new ObservableCollection<TreeModel>();
            IsChecked = false;
            IsExpanded = false;
            IconPath = "";
            VisibilityCheckBox = Visibility.Visible;
        }
        public void SetChildrenChecked(TreeModel child,bool isChecked)
        {
            child.IsChecked = isChecked;
            foreach (TreeModel child_ in child.Children)
            {
                SetChildrenChecked(child_,isChecked);
            }
        }
        public void SetChildrenExpanded(TreeModel child,bool isExpanded)
        {
            child.IsExpanded = isExpanded;
            foreach (TreeModel child_ in child.children)
            { 
                child_.SetChildrenExpanded(child_,isExpanded);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e) {
            PropertyChanged?.Invoke(this, e);
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
        
    }
}
